package com.app.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.app.common.Result;
import com.app.model.ActivityModel;
import com.app.model.BusinessActivityModel;
import com.app.service.BusinessActivityService;
import com.app.utils.CommonHelp;
import com.app.vo.ActivityVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/businessActivity")
public class BusinessActivityController {
    @Autowired
    private BusinessActivityService businessActivityService;

    //查询此商家已绑定的活动
    @GetMapping("/getBusinessActivity")
    public Result getBusinessActivity(Integer businessId,Integer classification) {
        ActivityVo businessActivityList = businessActivityService.getBusinessActivity(businessId,classification);
        return Result.success(businessActivityList);
    }

    //商家添加活动
    @PostMapping("/businessAddActivity")
    public Result businessAddActivity(@RequestBody BusinessActivityModel businessActivityModel) {
        Integer activityId = businessActivityModel.getActivityId();
        Integer businessId = businessActivityModel.getBusinessId();
        QueryWrapper<BusinessActivityModel> qw = new QueryWrapper<>();
        qw.eq("business_id", businessId);
        qw.eq("activity_id", activityId);
        List<BusinessActivityModel> list = businessActivityService.list(qw);
        if (CollectionUtil.isNotEmpty(list)) {
            return Result.error(201, "此活动已被此商家添加,请勿重复添加");
        }
        businessActivityModel.setCreateTime(CommonHelp.getNowTimestamp());
        businessActivityService.save(businessActivityModel);
        return Result.success("添加活动成功");
    }

    //商家取消活动
    @PostMapping("/businessStopActivity")
    public Result businessStopActivity(@RequestBody BusinessActivityModel businessActivityModel) {
        Integer activityId = businessActivityModel.getActivityId();
        Integer businessId = businessActivityModel.getBusinessId();
        QueryWrapper<BusinessActivityModel> qw = new QueryWrapper<>();
        qw.eq("business_id", businessId);
        qw.eq("activity_id", activityId);
        businessActivityService.remove(qw);
        return Result.success("商家停止此活动成功");
    }
}
